package org.shj.jvm;

import java.util.Vector;

/**
 * Vector 不是绝对线程安全的
 * @author Administrator
 *
 */
public class VectorTest {
	public static Vector<Integer> vector = new Vector<Integer>();

	public static void main(String[] args) {
		while(true){
			for(int i = 0 ; i < 100; i++){
				vector.add(i);
			}
			
			Thread removeThread = new Thread(() -> {
				try{
					for(int i = 0 ; i < vector.size(); i++){
						vector.remove(i);
					}
				}catch(RuntimeException e){
					e.printStackTrace();
					System.exit(-1);
				}
			});
			
			Thread printThread = new Thread(() -> {
				try{
					for(int i = 0; i< vector.size(); i++){
						System.out.println(vector.get(i));
					}
				}catch(RuntimeException e){
					e.printStackTrace();
					System.exit(-1);
				}
			});
			
			removeThread.start();
			printThread.start();
			
			while(Thread.activeCount() > 20); //防止产生过多的线程
		}
	}

}
